{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/58_add_vector.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "# How to add a shapefile, GeoJSON, and KML to the map\n",
    "\n",
    "## For ipyleaflet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# geemap.update_package()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[0, 0], zoom=2)\n",
    "\n",
    "in_geojson = \"https://raw.githubusercontent.com/gee-community/geemap/master/examples/data/cable_geo.geojson\"\n",
    "Map.add_geojson(in_geojson, layer_name=\"Cable lines\")\n",
    "\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[0, 0], zoom=2)\n",
    "url = \"https://raw.githubusercontent.com/gee-community/geemap/master/examples/data/countries.geojson\"\n",
    "Map.add_geojson(\n",
    "    url, layer_name=\"Countries\", fill_colors=[\"red\", \"yellow\", \"green\", \"orange\"]\n",
    ")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "Map = geemap.Map(center=[0, 0], zoom=2)\n",
    "\n",
    "url = \"https://raw.githubusercontent.com/gee-community/geemap/master/examples/data/countries.geojson\"\n",
    "\n",
    "\n",
    "def random_color(feature):\n",
    "    return {\n",
    "        \"color\": \"black\",\n",
    "        \"fillColor\": random.choice([\"red\", \"yellow\", \"green\", \"orange\"]),\n",
    "    }\n",
    "\n",
    "\n",
    "Map.add_geojson(url, layer_name=\"Countries\", style_callback=random_color)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[0, 0], zoom=2)\n",
    "\n",
    "url = \"https://raw.githubusercontent.com/gee-community/geemap/master/examples/data/countries.geojson\"\n",
    "\n",
    "style = {\n",
    "    \"stroke\": True,\n",
    "    \"color\": \"#0000ff\",\n",
    "    \"weight\": 2,\n",
    "    \"opacity\": 1,\n",
    "    \"fill\": True,\n",
    "    \"fillColor\": \"#0000ff\",\n",
    "    \"fillOpacity\": 0.1,\n",
    "}\n",
    "\n",
    "hover_style = {\"fillOpacity\": 0.7}\n",
    "\n",
    "Map.add_geojson(url, layer_name=\"Countries\", style=style, hover_style=hover_style)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://raw.githubusercontent.com/gee-community/geemap/master/examples/data/countries.zip\"\n",
    "geemap.download_file(url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "Uncomment to install `PyCRS` as needed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install PyCRS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[0, 0], zoom=2)\n",
    "\n",
    "in_shp = \"countries.shp\"\n",
    "Map.add_shapefile(in_shp, layer_name=\"Countries\")\n",
    "\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "in_kml = \"https://raw.githubusercontent.com/gee-community/geemap/master/examples/data/us_states.kml\"\n",
    "Map.add_kml(in_kml, layer_name=\"US States KML\")\n",
    "\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[0, 0], zoom=2)\n",
    "url = \"https://raw.githubusercontent.com/gee-community/geemap/master/examples/data/countries.geojson\"\n",
    "Map.add_vector(\n",
    "    url, layer_name=\"Countries\", fill_colors=[\"red\", \"yellow\", \"green\", \"orange\"]\n",
    ")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15",
   "metadata": {},
   "source": [
    "## For folium"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geemap.foliumap as geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "in_shp = \"countries.shp\"\n",
    "in_geojson = \"https://raw.githubusercontent.com/gee-community/geemap/master/examples/data/us_states.json\"\n",
    "in_kml = \"./us_states.kml\"  # the kml visualization above downloads this file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_shapefile(in_shp, layer_name=\"Shapefile\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_geojson(in_geojson, layer_name=\"GeoJSON\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_kml(in_kml, layer_name=\"KML\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
